/**
 * @Project: Runer
 * @Title: SDCardUtil.java
 * @Package com.runlooker.runer.util
 * @author town
 * @date 2014-5-8 下午10:14:14
 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved.
 * @version V1.0
 */
package com.runlooker.runer.util;

import java.io.File;

import android.os.Environment;

/**
 * sdcard工具
 * 
 * 
 * @ClassName SDCardUtil
 * @Description 模块描述
 * @author town
 * @Date 2014-5-8
 */
public class SDCardUtil {
	/**
	 * @return 查看是否安装了SD卡
	 */
	public static boolean isExistSDCard() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	public static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED); // 判断sd卡是否存在

		if (sdCardExist) // 如果SD卡存在，则获取跟目录
		{
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}
		return sdDir.toString();

	}

	/* Checks if external storage is available for read and write */
	public static boolean isExternalStorageWritable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		}
		return false;
	}

	/* Checks if external storage is available to at least read */
	public static boolean isExternalStorageReadable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)
				|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * 获得SD卡的总容量(MB)
	 * 
	 * @return
	 */
	public static Long getSDCardTotalCapacity() {
		// 取得SDCard当前的状态
		long nSDFreeSize = 0;
		String sDcString = Environment.getExternalStorageState();

		if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {

			// 取得sdcard文件路径
			File pathFile = android.os.Environment
					.getExternalStorageDirectory();

			android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());

			// 获取SDCard上BLOCK总数
			long nTotalBlocks = statfs.getBlockCount();

			// 获取SDCard上每个block的SIZE
			long nBlocSize = statfs.getBlockSize();

			// 获取可供程序使用的Block的数量
			long nAvailaBlock = statfs.getAvailableBlocks();

			// 计算SDCard 总容量大小MB
			nSDFreeSize = nTotalBlocks * nBlocSize / 1024 / 1024;

		}
		return nSDFreeSize;
	}

	/**
	 * 
	 * 获得SD卡的空余容量(MB)
	 * 
	 * @return
	 */
	public static Long getSDCardSurplusCapacity() {
		// 取得SDCard当前的状态
		long nSDTotalSize = 0;
		String sDcString = Environment.getExternalStorageState();

		if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {

			// 取得sdcard文件路径
			File pathFile = android.os.Environment
					.getExternalStorageDirectory();

			android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());

			// 获取SDCard上BLOCK总数
			long nTotalBlocks = statfs.getBlockCount();

			// 获取SDCard上每个block的SIZE
			long nBlocSize = statfs.getBlockSize();

			// 获取可供程序使用的Block的数量
			long nAvailaBlock = statfs.getAvailableBlocks();

			// 计算SDCard 总容量大小MB
			nSDTotalSize = nAvailaBlock * nBlocSize / 1024 / 1024;

		}
		return nSDTotalSize;
	}
}
